package cn.edu.zjicm.wordsnet_d.adapter;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView;
import cn.edu.zjicm.wordsnet_d.R;
import cn.edu.zjicm.wordsnet_d.activity.WordDetailActivity;
import cn.edu.zjicm.wordsnet_d.activity.WordsBookActivity;
import cn.edu.zjicm.wordsnet_d.adapter.common.SortedListAdapter;
import cn.edu.zjicm.wordsnet_d.app.WordApp;
import cn.edu.zjicm.wordsnet_d.data.DataManager;
import cn.edu.zjicm.wordsnet_d.data.OneWord;
import cn.edu.zjicm.wordsnet_d.util.TypefaceUtil;
import cn.edu.zjicm.wordsnet_d.view.WordSummaryView;

public class WordListAdapter extends SortedListAdapter<OneWord> {
	private WordsBookActivity activity;
	private LayoutInflater mInflater;
	private View view;
	private boolean isDefaultHide = false;

	public WordListAdapter(WordsBookActivity activity, ArrayList<OneWord> list) {
		this(activity, list, false);
	}

	public WordListAdapter(WordsBookActivity activity, ArrayList<OneWord> list, boolean isDefaultHide) {
		super(list);
		this.activity = activity;
		this.isDefaultHide = isDefaultHide;
		this.mInflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			view = mInflater.inflate(R.layout.view_word_note_item, null);
			WordSummaryView holder = new WordSummaryView();
			holder.word = (TextView) view.findViewById(R.id.word_note_item_word);
			holder.phe = (TextView) view.findViewById(R.id.word_note_item_phe);
			TypefaceUtil.getInstance(activity).setTypeface(holder.phe);
			holder.traslation = (TextView) view.findViewById(R.id.word_note_item_translation);
			holder.wordCheckBox = (CheckBox) view.findViewById(R.id.word_note_item_checkbox);
			view.setTag(holder);
			convertView = view;
		}
		final OneWord word = getItem(position);
		convertView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				WordDetailActivity.startActivity(activity, word.getIndex_word_id());
			}
		});

		final WordSummaryView holder = (WordSummaryView) convertView.getTag();
		if (!activity.showChineseInDifficult && isDefaultHide) {
			holder.phe.setVisibility(View.GONE);
			holder.traslation.setVisibility(View.GONE);
		} else {
			holder.phe.setVisibility(View.VISIBLE);
			holder.traslation.setVisibility(View.VISIBLE);
			holder.phe.setText(word.getPhonetic());
			holder.traslation.setText(word.getSensesSenior());
		}
		holder.word.setText(word.getLemma());

		holder.wordCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (isChecked && !(WordsBookActivity.wordList.contains(word))) {
					WordsBookActivity.wordList.add(word);
				} else if (!isChecked && (WordsBookActivity.wordList.contains(word))) {
					WordsBookActivity.wordList.remove(word);
				}
				if (WordsBookActivity.getInstance() != null && WordsBookActivity.getInstance().mPagerAdapter != null)
					activity.setFamiliarSetButton();
			}
		});

		// if (mDegree == OneWord.DEGREE_UNFAMILIAR) {
		// holder.wordCheckBox.setVisibility(View.GONE);
		// } else if (mDegree == OneWord.DEGREE_FAMILIAR &&
		// word.getSense_degree_fm() < OneWord.SENSE_DEGREE_TOO_EASY) {
		if (activity.edit) {
			holder.wordCheckBox.setVisibility(View.VISIBLE);
			holder.wordCheckBox.setChecked(WordsBookActivity.wordList.contains(word));
		} else {
			holder.wordCheckBox.setVisibility(View.GONE);
		}
		// } else if (mDegree == OneWord.DEGREE_FAMILIAR &&
		// word.getSense_degree_fm() >= OneWord.SENSE_DEGREE_TOO_EASY) {
		// if (activity.edit) {
		// holder.wordCheckBox.setVisibility(View.VISIBLE);
		// holder.wordCheckBox.setChecked(WordsBookActivity.wordList.contains(word));
		// } else {
		// holder.wordCheckBox.setVisibility(View.GONE);
		// }
		// }

		return convertView;
	}

	protected void dialog(boolean toFamiliar, final OneWord word) {
		AlertDialog.Builder builder = new Builder(activity);
		if (toFamiliar) {

		} else {
			builder.setMessage("确定将此单词移至生词列表？");
			builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
					DataManager.getInstance().changeFamiliarToUnFamiliar(word, activity);
					WordApp.getInstance().notifyInstantiateItem(2);
					WordApp.getInstance().notifyInstantiateItem(1);
				}
			});
		}
		builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
}
